package cz.cvut.liy.adapter;

import java.util.List;

import cz.cvut.liy.data.Question;
import cz.cvut.liy.data.Test;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class TestAdapter extends BaseExpandableListAdapter{

	List<Test> test;
	String[] menu = {"Start", "Continue", "Statistics", "Delete"};
	Context context;
	
	public TestAdapter(List<Test> test, Context context) {
		this.test = test;
		this.context = context;
		// TODO Auto-generated constructor stub
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		Test test = (Test) getGroup(groupPosition);
		if (!test.isStarted() && childPosition > 0) childPosition++;
		return menu[childPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView tv = getGenericView();
		tv.setText(getChild(groupPosition, childPosition).toString());
		return tv;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		Test test = (Test) getGroup(groupPosition);
		int count = menu.length;
		if (!test.isStarted()) count--; 
		return count;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return test.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return test.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return (long) groupPosition;
	}
	
	public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 50);
         
        TextView textView = new TextView(context);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView tv = getGenericView();
		Test test = (Test) getGroup(groupPosition);
		
		tv.setText(getGroup(groupPosition).toString()+" "+test.getAnsveredQuestions()+"/"+test.getQuestions().size());
		return tv;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
